package com.caigou.modules.vote.repository

import com.caigou.comm.base.bean.CommentBean
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.bean.HttpResult
import com.caigou.modules.vote.bean.GuessBean
import com.caigou.modules.vote.bean.RunDogBean
import com.caigou.modules.vote.bean.UnlikeBean
import com.caigou.modules.vote.network.HttpVoteService
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/09/26
 */
@ActivityRetainedScoped
class VoteRepository @Inject constructor() {

    fun getPeriodList(year: Int, type: Int = 1): Flow<HttpResult<MutableList<String>>> {
        return HttpVoteService.service.getPeriodList(year, type)
    }

    fun getRunDogDetail(year: Int, period: Int, userId: String): Flow<HttpResult<RunDogBean>> {
        return HttpVoteService.service.getRunDogDetail(year, period, userId)
    }

    fun getGuessDetail(year: Int, period: Int, userId: String): Flow<HttpResult<GuessBean>> {
        return HttpVoteService.service.getGuessDetail(year, period, userId)
    }

    fun getUnlikeDetail(year: Int, period: Int, userId: String): Flow<HttpResult<UnlikeBean>> {
        return HttpVoteService.service.getUnlikeDetail(year, period, userId)
    }

    fun getCommentList(page: Int, limit: Int, dataId: Int, type: Int, sort: Int, currentUserId: String): Flow<HttpResult<CommentBean>> {
        return when (type) {
            1 -> HttpVoteService.service.getRunDogCommentList(page, limit, dataId, sort, currentUserId)
            2 -> HttpVoteService.service.getGuessCommentList(page, limit, dataId, sort, currentUserId)
            else -> HttpVoteService.service.getUnlikeCommentList(page, limit, dataId, sort, currentUserId)
        }
    }

    fun getCommentDetail(dataId: Int, replyId: Int, type: Int, userId: String): Flow<HttpResult<CommentItemBean>> {
        return when (type) {
            1 -> HttpVoteService.service.getRunDogCommentDetail(dataId, replyId, userId)
            2 -> HttpVoteService.service.getGuessCommentDetail(dataId, replyId, userId)
            else -> HttpVoteService.service.getUnlikeCommentDetail(dataId, replyId, userId)
        }
    }

    fun setReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, dataId: Int, type: Int): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("dataId", dataId.toString())
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return when (type) {
            1 -> HttpVoteService.service.setRunDogReply(body.build())
            2 -> HttpVoteService.service.setGuessReply(body.build())
            else -> HttpVoteService.service.setUnlikeReply(body.build())
        }
    }

    fun setCommentReply(
        content: String, files: MutableList<File>, senderId: String,
        receiverId: String, dataId: Int, parentId: Int, type: Int
    ): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("dataId", dataId.toString())
            .addFormDataPart("parentId", parentId.toString())
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return when (type) {
            1 -> HttpVoteService.service.setRunDogReply(body.build())
            2 -> HttpVoteService.service.setGuessReply(body.build())
            else -> HttpVoteService.service.setUnlikeReply(body.build())
        }
    }

    fun setVoteLike(dataId: Int, senderId: String, type: Int): Flow<HttpResult<String>> {
        val jsonObjects = JSONObject()
        jsonObjects.put("dataId", dataId)
        jsonObjects.put("senderId", senderId)
        val body = jsonObjects.toString().toRequestBody()

        return when (type) {
            1 -> HttpVoteService.service.setRunDogLike(body)
            2 -> HttpVoteService.service.setGuessLike(body)
            else -> HttpVoteService.service.setUnlikeLike(body)
        }
    }

    fun setCommentLike(replyId: Int, senderId: String, receiverId: String, type: Int): Flow<HttpResult<String>> {
        val jsonObjects = JSONObject()
        jsonObjects.put("replyId", replyId)
        jsonObjects.put("senderId", senderId)
        jsonObjects.put("receiverId", receiverId)
        val body = jsonObjects.toString().toRequestBody()

        return when (type) {
            1 -> HttpVoteService.service.setRunDogCommentLike(body)
            2 -> HttpVoteService.service.setGuessCommentLike(body)
            else -> HttpVoteService.service.setUnlikeCommentLike(body)
        }
    }
}